<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin extends Controller_Template {

    public $template = 'admin_template';

    public function before() {
        parent::before();
        if (!Auth::instance()->logged_in('admin') AND Request::current()->uri() !== 'login') {
            $this->request->redirect('/login');
        }
    }
    
    public function action_index() {       
        $data['username'] = "Administrator";
        $this->template->title = "Админская панель";
        $this->template->content = View::factory('admin', $data);
    }
    
    public function action_save() {
        $data = array();
        $model = ORM::factory('rookeeaccount', 1);
        $model->values(array(
            'username' => $_POST['username'],
            'password' => $_POST['password'],
        ));
        try {
            if ($model->loaded()) {
                $model->update();
            } else {
                $model->save();
            }
            $data['ok'] = "";
        } catch (ORM_Validation_Exception $e) {
            $data['error'] = $e->errors('models/account');
        }
        $data['username'] = "Administrator"; 
        $this->template->project_id = 0;
        $this->template->title = "Админская панель";
        $this->template->content = View::factory('admin', $data);
    }

}